package org.opens.lambda.collector;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamOfFlatMap {

    /**
     * 说明:
     *      1. flatMap相当于是将多个流进行合并, 合并为一个新的流.
     */
    @Test
    public void testFlatMap() {
        String[] strArr = {"Hello","World"};
        Stream<String[]> stream = Arrays.stream(strArr).map(x -> x.split(""));
        System.out.println(stream);
        Stream<String> stringStream = Arrays.stream(strArr).flatMap(x -> Arrays.stream(x.split("")));
        List<String> collect = stringStream.collect(Collectors.toList());
        System.out.println(collect);
    }

    @Test
    public void test2() {
        String x1 = "1,2,3";
        String x2 = "11,12,13,1,2,3";
        List<String> stringList = Arrays.asList(x1, x2);
        List<Integer> collect = stringList.stream().flatMap(x -> Arrays.stream(x == null ? new String[]{} : x.split(",")))
                .map(Integer::valueOf)
                .distinct()
                .collect(Collectors.toList());
        System.out.println(collect);
    }

}
